<template>
  <el-card
    shadow="always"
    :body-style="{ padding: '20px' }"
    :style="{ marginTop: '20px' }"
    class="middle-view"
  >
    <template #header>
      <el-menu
        mode="horizontal"
        :default-active="state.activeIndex"
        @select="handleSelect"
      >
        <el-menu-item index="1">销售额</el-menu-item>
        <el-menu-item index="2">访问量</el-menu-item>
      </el-menu>
      <div class="right">
        <el-radio-group v-model="state.time" size="large">
          <el-radio-button label="今日" />
          <el-radio-button label="本周" />
          <el-radio-button label="本月" />
          <el-radio-button label="今年" />
        </el-radio-group>
        <el-date-picker
          v-model="state.time2"
          type="daterange"
          :shortcuts="shortcuts"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </div>
    </template>
    <div class="content">
      <v-chart :option="option()" autoresize></v-chart>
      <div class="rank-list">
        <div class="title"></div>
        <div class="list">
          <div class="list-item" v-for="item in renderData" :key="item.no">
            <div class="no" :class="{ 'top-no': item.no <= 3 }">
              {{ item.no }}
            </div>
            <div class="name">{{ item.name }}</div>
            <div class="count">{{ item.count }}</div>
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script lang="ts">
import { defineComponent, ref } from "vue";

export default defineComponent({
  name: "MiddleViews",
});
</script>

<script setup lang="ts">
import { computed } from "vue";
import useReportDataStore from "@/stores/reportData";

const reportDataStore = useReportDataStore();

const orderFullYearAxis = computed(
  () => reportDataStore.reportData?.orderFullYearAxis
);
const orderFullYear = computed(() => reportDataStore.reportData?.orderFullYear);
const userFullYearAxis = computed(
  () => reportDataStore.reportData?.userFullYearAxis
);
const userFullYear = computed(() => reportDataStore.reportData?.userFullYear);
const orderRank = computed(() => reportDataStore.reportData?.orderRank);
const userRank = computed(() => reportDataStore.reportData?.userRank);

const renderData = computed(() =>
  state.value.activeIndex === "1" ? orderRank.value : userRank.value
);

const state = ref({
  activeIndex: "1",
  time: "今日",
  time2: "",
});

const shortcuts = [
  {
    text: "最近一周",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 1000 * 60 * 60 * 24 * 7);
      return [start, end];
    },
  },
  {
    text: "最近一个月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
      return [start, end];
    },
  },
  {
    text: "最近三个月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
      return [start, end];
    },
  },
];

const option = () => {
  return {
    xAxis: {
      type: "category",
      data:
        state.value.activeIndex === "1"
          ? orderFullYearAxis.value
          : userFullYearAxis.value,
      boundaryGap: true,
      axisTick: {
        alignWithLabel: true, // 类目轴中在 boundaryGap 为 true 的时候有效，可以保证刻度线和标签对齐
      },
    },
    yAxis: {
      type: "value",
      // 横向辅助线
      splitLine: {
        show: true,
        lineStyle: {
          type: "dashed",
          width: 1,
          color: "yellow",
        },
      },
    },
    series: [
      {
        type: "bar",
        barWidth: "50%",
        data:
          state.value.activeIndex === "1"
            ? orderFullYear.value
            : userFullYear.value,
        itemStyle: {
          color: "khaki",
        },
      },
    ],
    grid: {
      left: 50,
      top: 50,
      bottom: 50,
      right: 50,
    },
    tooltip: {
      triiger: "axis",
      confine: true,
    },
  };
};

const handleSelect = (key: string) => {
  state.value.activeIndex = key;
};
</script>

<style scoped lang="less">
.middle-view {
  margin-top: 20px;

  :deep(.el-card__header) {
    // display: flex;
    position: relative;
    border-bottom: none;
    padding: 0 20px;

    .right {
      position: absolute;
      top: 0;
      right: 20px;
      height: 100%;
      display: flex;
      align-items: center;

      .el-date-editor {
        margin-left: 20px;
      }
    }
  }

  .content {
    display: flex;
    height: 270px;

    .echarts {
      flex: 5;
    }

    .rank-list {
      flex: 2;

      .title {
        font-size: 14px;
        color: #333;
        font-weight: 500;
        margin-top: 20px;
      }

      .list {
        margin-top: 15px;

        .list-item {
          display: flex;
          align-items: center;
          height: 20px;
          font-size: 12px;
          margin: 6px 20px 6px 0;

          .no {
            width: 20px;
            height: 20px;
            color: #333;
            display: flex;
            justify-content: center;
            align-items: center;

            &.top-no {
              border-radius: 50%;
              background-color: #000;
              color: #fff;
              font-weight: 300;
            }
          }

          .name {
            margin-left: 10px;
          }

          .count {
            flex: 1;
            text-align: right;
          }
        }
      }
    }
  }
}
</style>
